//using Microsoft.VisualBasic;
using System;

using System.Diagnostics;
using log4net;
using NHibernate;
using NHibernate.Cfg;
using System.Collections;
namespace NCS
{

	class theProgram
	{


		static internal void Main(string[] args)
		{
			//childDel()
			//childAdd()
			//testCurveData()
			//newmethod()
			//newSample()
			//newChanel()
			//newusehistory()
			//newcurvehistory()
			//newweight()
			//newdichanel()
			//newdochanel()
			//testuser1()
			//Dim f As New Form1
			//f.ShowDialog()
			//testvoltnew()
			testcalcField();
		}
		private static void testcalcField()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			int nvlv = 0;
			object o = null;
			o = session.CreateQuery("select max(dc.id) from doChanel dc").UniqueResult();
			if ((o != null)) {
				nvlv = Convert.ToInt32(o) + 1;
			} else {
				nvlv = 0;
			}
			//Dim s As sample
			//s = New sample
			//s.c = 0.1
			//session.save(s)
			//session.flush()
		}

		private static void testuser1()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			IList o = null;
			o = session.CreateCriteria(typeof(muser)).List();

		}

		private static void testvoltnew()
		{
			//Dim configuration As Configuration = Class1.configuration
			//Dim factory As ISessionFactory = configuration.BuildSessionFactory()
			//Dim session As ISession = factory.OpenSession()
			//'Dim o As IList
			//'Dim v As VoltNew
			//'v = TryCast(session.Get(GetType(VoltNew), 18), VoltNew)
			//Dim mysig As Sig
			//mysig.Initialize()
			//loadsignew(session, mysig, 18)
			//savesignew(session, mysig, -1)
			//loadsignew(session, mysig, -1)
		}

		//usehistory
		//CurveHis
		//diChanel
		private static void newnetset()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			netSet newmethod1 = new netSet();
			newmethod1.ip = "127.0.0.1";
			newmethod1.id = 18;
			session.Save(newmethod1);
			session.Flush();
		}

		private static void newdochanel()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			doChanel newmethod1 = new doChanel();
			newmethod1.cname = "sample1";
			newmethod1.id = 18;
			session.Save(newmethod1);
			session.Flush();
		}
		private static void newdichanel()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			diChanel newmethod1 = new diChanel();
			newmethod1.cname = "sample1";
			newmethod1.id = 18;
			session.Save(newmethod1);
			session.Flush();
		}

		private static void newweight()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			weight newmethod1 = new weight();
			newmethod1.name = "sample1";
			session.Save(newmethod1);
			session.Flush();
		}
		private static void newcurvehistory()
		{
			//Dim configuration As Configuration = Class1.configuration
			//Dim factory As ISessionFactory = configuration.BuildSessionFactory()
			//Dim session As ISession = factory.OpenSession()
			//Dim newmethod1 As New CurveHis
			//newmethod1.ele = "C"
			//session.Save(newmethod1)
			//session.Flush()
		}

		private static void newusehistory()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			usehistory newmethod1 = new usehistory();
            newmethod1.endtime = System.DateTime.Now; ;
			session.Save(newmethod1);
			session.Flush();
		}
		private static void newSample()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
            //SampleAve newmethod1 = new SampleAve();
            //newmethod1.name = "a";
            //session.Save(newmethod1);
            //session.Flush();
		}
		private static void newChanel()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			chanel newmethod1 = new chanel();
			newmethod1.ele = "a";
			session.Save(newmethod1);
			session.Flush();
		}
		private static void newmethod()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			method newmethod1 = new method();
			newmethod1.cname = "all casade";
			newmethod1.cuser = "general";
			session.Save(newmethod1);
			session.Flush();
		}
		private static void testCurveData()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			NCS.curveData cdata = null;
			//m = DirectCast(session.CreateSQLQuery("select * from curvedata").AddEntity(GetType(NCS.curveData)).List()(0), NCS.curveData)
			//m.jieqi = False
			//m.flushtime = 6
			//m.curveXishuList(0).x0 = 0.9
			cdata = new curveData();
			curveXishu cxishu = null;
			cxishu = new NCS.curveXishu();
			cxishu.x0 = 0;
			cxishu.x1 = 0;
			cxishu.ele = "C";
			cxishu.cishu = 1;
			method newmethod1 = new method();
			newmethod1.cname = "all casade";
			newmethod1.cuser = "general";




			cxishu.method = newmethod1;
			newmethod1.curveXishuList.Add(cxishu);
			//session.Save(cxishu)
			//session.Flush()
			cdata.curveXishu = cxishu;
			cxishu.curveDataList.Add(cdata);
			//session.Save(cdata)
			session.Save(newmethod1);
			session.Flush();
		}
		private static void childAdd()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			NCS.method m = null;
			m = (NCS.method)session.CreateSQLQuery("select * from method where cuser='cs' and cname='gen'").AddEntity(typeof(NCS.method)).List()[0];
			//m.jieqi = false;
			//m.flushtime = 6;
			//m.curveXishuList[0].x0 = 0.9;
			NCS.curveXishu x1 = new NCS.curveXishu();
			x1.ele = "C";
			//x1.methodid = m.ID;
			x1.method = m;
			m.curveXishuList.Add(x1);
			session.Save(x1);
			session.Flush();
		}


		private static void childDel()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			NCS.method m = null;
			m = (NCS.method)session.CreateSQLQuery("select * from method where cuser='cs' and cname='gen'").AddEntity(typeof(NCS.method)).List()[0];
			//m.jieqi = false;
			//m.flushtime = 6;
			//m.curveXishuList[0].x0 = 0.9;
			NCS.curveXishu x1 = null;
			x1 = m.curveXishuList[0];
			m.curveXishuList.Remove(x1);
			//x1.method = null;
			session.Delete(x1);
			session.Flush();
		}

		private static void testMethod()
		{
			Configuration configuration = Class1.configuration;
			ISessionFactory factory = configuration.BuildSessionFactory();
			ISession session = factory.OpenSession();
			NCS.method m = null;
			m = (NCS.method)session.CreateSQLQuery("select * from method where cuser='cs' and cname='gen'").AddEntity(typeof(NCS.method)).List()[0];
			m.jieqi = false;
			m.flushtime = 6;
			m.curveXishuList[0].x0 = 0.9;
			session.Flush();
		}

		private static void testUser()
		{
			// ----------------------------------------
			// Step 1: log4net configuration (optional)
			// ----------------------------------------
			//
			// The first step is to configure log4net, an open source logging
			// framework that can be found at http://logging.apache.org/log4net/.
			// NHibernate uses log4net to output warnings, debug messages, etc.
			// It is strongly recommended that you enable log4net when
			// troubleshooting your application with NHibernate.
			//
			// The statement below instructs log4net to configure itself
			// by loading its settings from the application configuration file.
			// See app.config for more details; however, it is beyond the scope
			// of this sample to discuss log4net in detail.  Refer to the
			// above web site to learn more.
			//
			// This particular sample is configured to write to a log file
			// called log-file.txt.  It will be created in the same folder
			// as the executable.

			log4net.Config.XmlConfigurator.Configure();


			// ---------------------
			// Step 2: Configuration
			// ---------------------
			//
			// A Configuration object manages global settings as well as
			// the mappings between your classes and the database.  You
			// will always create a Configuration object.

			Configuration configuration = new Configuration();

			// The Configure method (without parameters) instructs NHibernate
			// to look for its configuration from app.config.  If no
			// configuration can be found, it then looks for an XML file
			// with the name hibernate.cfg.xml.  This sample uses the XML
			// file instead of app.config.
			//
			// Note: hibernate.cfg.xml has been added as a content file to
			// this project.  The file is configured to be copied to the
			// output folder (check the Copy to Output Directory property
			// of the file from the Solution Explorer panel).

			Console.WriteLine("Loading hibernate.cfg.xml...");
			configuration.Configure();

			// The next step is to load and parse all of the files that
			// define the mappings between your objects and the underlying
			// database.  There are many ways to do this (refer to the
			// NHibernate documentation).  The technique below instructs
			// NHibernate to search for all mapping files that have
			// been embedded in the assembly.

			Console.WriteLine("Loading mapping files in this executable...");
			configuration.AddAssembly(System.Reflection.Assembly.GetExecutingAssembly());


			// --------------------------------
			// Step 3: Create a Session Factory
			// --------------------------------
			//
			// An ISessionFactory is used to create NHibernate sessions.  A 
			// session represents a connection to the database with all of
			// the functionality provided by NHibernate.  

			ISessionFactory factory = configuration.BuildSessionFactory();


			// ---------------------
			// Test 1: Create a muser
			// ---------------------

			string firstId = null;

			using (ISession session = factory.OpenSession()) {

				Console.WriteLine("Saving a test muser object to the database...");

				muser user = new muser();
				user.mname = "hello3";
				//user.EmailAddress = TestEmailAddress;
				//user.Password = TestPassword;
				//user.UserName = TestUserName;

				// Save the user object, then flush the session to
				// commit it to the database.  Upon return, the
				// user object should have a new ID.

				session.Save(user);
				session.Flush();

				firstId = user.mname;

				Console.WriteLine("id" + user.ID.ToString());

				//Debug.Assert(
				//    firstId > 0, "The Id should have been returned.");

				// If you get a failure here (e.g. because user.Id is zero),
				// make sure the hibernate.connection.release_mode setting
				// in hibernate.cfg.xml is set to "on_close".
				//
				// Explanation:  The Id of the underlying Users table is an
				// AutoNumber. This means Access/Jet assigns the value of
				// the ID when the record is first created.  The Jet driver
				// for NHibernate retrieves the new ID by sending a special
				// SQL statement ("select @@identity").  The @@Identity 
				// variable contains the last ID generated by Jet.  
				//
				// However, NHibernate is aggressive in closing the
				// ADO.NET database connection between SQL statements.  This
				// is the correct design (normally) but has the unfortunate
				// side-effect of invalidating the @@Identity variable.
				// NHibernate closes the connection between the INSERT
				// statment (when saving) and the SELECT @@Identity statement.
				//
				// The temporary work around is to enable the older behavior
				// of forcing NHibernate to keep the underlying connection open
				// throughout the lifetime of the session.  This will likely
				// be fixed in a future version of the library.
				Console.WriteLine("The muser object was assigned an ID of " + firstId + "!");
			}


			// ---------------------
			// Test 2: Load the User
			// ---------------------
			//
			// Try reloading the user back from the database.  Compare
			// the fields with the properties originally saved.  This is
			// performed with a completely different session and User object
			// to show that it was truly loaded from the database.

			using (ISession session = factory.OpenSession()) {

				Console.WriteLine("Attempting to reload User with ID " + firstId + "...");

				muser user = session.Load<muser>(firstId);
				Console.WriteLine("id" + user.ID.ToString());
				Debug.Assert(user.mname == firstId, "The wrong Id field was returned.");
				user = session.Load<muser>(firstId);
				Console.WriteLine("id" + user.ID.ToString());
				//Debug.Assert(
				//    user.EmailAddress == TestEmailAddress,
				//    "The wrong EmailAddress was returned.");

				//Debug.Assert(
				//    user.Password == TestPassword,
				//    "The wrong Password was returned.");

				//Debug.Assert(
				//    user.UserName == TestUserName,
				//    "The wrong UserName was returned.");


				Console.WriteLine("The user was reloaded successfully!");
			}


			// -----------------------
			// Test 3: Delete the muser
			// -----------------------

			using (ISession session = factory.OpenSession()) {

				// Load the user again (to get an object reference),
				// then delete it from the database.

				Console.WriteLine("Deleting the user...");
				muser user = session.Load<muser>(firstId);
				session.Delete(user);

				// Now try to reload the user.  This should fail.

				try {
					Console.WriteLine("Confirming the user record is deleted...");
					muser deletedUser = session.Get<muser>(firstId);
					throw new InvalidOperationException("The load should not have succeeded.");
				} catch (ObjectDeletedException ex) {
					// eat
					Console.WriteLine(ex.Message);
				}

				session.Flush();
			}


			// I hope this sample was useful.  Free free to contact
			// me at dave@fairmetric.com if you have questions or feedback.

			Console.WriteLine("Done.  Press Enter to exit...");
			Console.ReadLine();

		}
	}
}
